package com.dao.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.JdbcTemplate;

import com.pojo.FuncBean;
import com.pojo.MenuBean;

@SuppressWarnings(value = { "unchecked" })
public class UserRoleDaoImpl 
{
	JdbcTemplate jdbcTmplt;

	public JdbcTemplate getJdbcTmplt()
	{
		return jdbcTmplt;
	}

	public void setJdbcTmplt(JdbcTemplate jdbcTmplt)
	{
		this.jdbcTmplt = jdbcTmplt;
	}

	public List getAllMenus()
	{
		String dosql = "select id,menuname,ordernum,url,isleaf,isstop,pmenu_id from MENUINFO order by ordernum";
		List<MenuBean> menus = jdbcTmplt.query(dosql, new MenuMapper());
		List<MenuBean> m1 = new ArrayList<MenuBean>();
		List<MenuBean> m2 = new ArrayList<MenuBean>();

		Map<Long,List<MenuBean>> m2map = new HashMap<Long,List<MenuBean>>();
		Map<Long,List<MenuBean>> m3map = new HashMap<Long,List<MenuBean>>();
		
		List<Integer> indexList = new ArrayList<Integer>();

		for(int i = 0; i < menus.size(); i++)
		{
			MenuBean menu =  menus.get(i);
			long pmenuId = menu.getPmenuId();
			if(pmenuId == 0)
			{
				m1.add(menu);
				indexList.add(i);
			}
		}
		
		for(int i = 0; i < indexList.size(); i++)
		{
			menus.remove(indexList.get(i));
		}
		
		indexList.clear();		
		
		for(int i = 0; i < m1.size(); i++)
		{
			MenuBean pmenu = m1.get(i);
			long id = pmenu.getId();
			List<MenuBean> menuList = new ArrayList<MenuBean>();
			for(int j = 0; j < menus.size(); j++)
			{
				MenuBean menu = menus.get(j);
				
				long pmenuId = menu.getPmenuId();
				if(pmenuId == id)
				{
					//System.out.print(menu.getName());
					m2.add(menu);
					menuList.add(menu);
					indexList.add(j);
				}
				
			}
			m2map.put(id, menuList);
		}
		
		for(int i = 0; i < indexList.size(); i++)
		{
			menus.remove(indexList.get(i));
		}
		
		for(int i = 0; i < m2.size(); i++)
		{
			MenuBean pmenu = m2.get(i);
			long id = pmenu.getId();
			List<MenuBean> menuList = new ArrayList<MenuBean>();
			for(int j = 0; j < menus.size(); j++)
			{
				MenuBean menu = menus.get(j);
				
				long pmenuId = menu.getPmenuId();
				if(pmenuId == id)
				{
					//System.out.print(menu.getName());
					menuList.add(menu);
				}
				
			}
			m3map.put(id, menuList);
		}
		
		List rList = new ArrayList();
		rList.add(m1);
		rList.add(m2map);
		rList.add(m3map);
		
		return rList;
	}

	public List getUserRole(String menuIds)
	{
		String dosql = "select id,menuname,ordernum,url,isleaf,isstop,pmenu_id from MENUINFO where id in"+
                       " (select distinct pmenu_id from menuinfo where id in ("+menuIds+") and pmenu_id is not null"+
                       " union"+
                       " select distinct pmenu_id from MENUINFO where id in (select distinct pmenu_id from menuinfo where id in ("+menuIds+") and pmenu_id is not null) and pmenu_id is not null)"+
                       " union"+
                       " select id,menuname,ordernum,url,isleaf,isstop,pmenu_id from MENUINFO where id in ("+menuIds+") order by ordernum";
		List<MenuBean> menus = jdbcTmplt.query(dosql, new MenuMapper());
		List<MenuBean> m1 = new ArrayList<MenuBean>();
		List<MenuBean> m2 = new ArrayList<MenuBean>();

		Map<Long,List<MenuBean>> m2map = new HashMap<Long,List<MenuBean>>();
		Map<Long,List<MenuBean>> m3map = new HashMap<Long,List<MenuBean>>();
		
		List<Integer> indexList = new ArrayList<Integer>();

		for(int i = 0; i < menus.size(); i++)
		{
			MenuBean menu =  menus.get(i);
			long pmenuId = menu.getPmenuId();
			if(pmenuId == 0)
			{
				m1.add(menu);
				indexList.add(i);
			}
		}
		
		for(int i = 0; i < indexList.size(); i++)
		{
			menus.remove(indexList.get(i));
		}
		
		indexList.clear();		
		
		for(int i = 0; i < m1.size(); i++)
		{
			MenuBean pmenu = m1.get(i);
			long id = pmenu.getId();
			List<MenuBean> menuList = new ArrayList<MenuBean>();
			for(int j = 0; j < menus.size(); j++)
			{
				MenuBean menu = menus.get(j);
				
				long pmenuId = menu.getPmenuId();
				if(pmenuId == id)
				{
					//System.out.print(menu.getName());
					m2.add(menu);
					menuList.add(menu);
					indexList.add(j);
				}
				
			}
			m2map.put(id, menuList);
		}
		
		for(int i = 0; i < indexList.size(); i++)
		{
			menus.remove(indexList.get(i));
		}
		
		for(int i = 0; i < m2.size(); i++)
		{
			MenuBean pmenu = m2.get(i);
			long id = pmenu.getId();
			List<MenuBean> menuList = new ArrayList<MenuBean>();
			for(int j = 0; j < menus.size(); j++)
			{
				MenuBean menu = menus.get(j);
				
				long pmenuId = menu.getPmenuId();
				if(pmenuId == id)
				{
					//System.out.print(menu.getName());
					menuList.add(menu);
				}
				
			}
			m3map.put(id, menuList);
		}
		
		List rList = new ArrayList();
		rList.add(m1);
		rList.add(m2map);
		rList.add(m3map);
		
		return rList;
	}

}
